﻿namespace PomodoroTool.Core
{
    #region

    using System;
    using System.Windows.Threading;

    #endregion

    internal class PomodoroTimer
    {
        #region Constants and Fields

        private readonly TimeSpan _interval;

        private readonly DispatcherTimer _timer;

        private Action _finishCallback;

        private DateTime _startTime;

        private State _state;

        private TimeSpan _pausedTimeLeft;

        #endregion

        #region Constructors and Destructors

        public PomodoroTimer(TimeSpan interval)
        {
            _interval = interval;
            _timer = new DispatcherTimer();
            _timer.Tick += this._timer_Tick;
            _state = State.Stopped;
        }

        #endregion

        #region Enums

        private enum State
        {
            Running,
            Stopped,
            Paused
        }

        #endregion

        #region Properties

        public TimeSpan TimeLeft
        {
            get
            {
                if (_state == State.Running)
                {
                    return _startTime.Add(_interval).Subtract(DateTime.Now);
                } else if(_state == State.Paused)
                {
                    return _pausedTimeLeft;
                }
                return _interval;
            }
        }

        public bool Stopped
        {
            get
            {
                return _state == State.Stopped;
            }
        }

        #endregion

        #region Public Methods

        public void Pause()
        {
            if (_state == State.Running)
            {
                _pausedTimeLeft = TimeLeft;

                _state = State.Paused;
                _timer.Stop();

                _timer.Interval = _pausedTimeLeft;
            }
        }

        public void Start()
        {
            if (_state == State.Stopped)
            {
                _timer.Stop();

                _startTime = DateTime.Now;
                _state = State.Running;

                _timer.Interval = TimeLeft;
                _timer.Start();
            }
            else if (_state == State.Paused)
            {
                _startTime = DateTime.Now.Subtract(_interval).Add(_pausedTimeLeft);
                _timer.Start();
                _state = State.Running;
            }
        }

        public void Stop()
        {
            _timer.Stop();
            _state = State.Stopped;
        }

        #endregion

        #region Methods

        private void _timer_Tick(object sender, EventArgs e1)
        {
            _timer.Stop();
            _state = State.Stopped;

            if (_finishCallback != null)
            {
                _finishCallback();
            }
        }

        #endregion

        public void SetCallback(Action callback)
        {
            _finishCallback = callback;
        }
    }
}